Welcome mapsend in the world of gbfile api.
authoroliskoli <oliskoli>
Wed, 6 Dec 2006 22:44:27 +0000 (22:44 +0000)
committeroliskoli <oliskoli>
Wed, 6 Dec 2006 22:44:27 +0000 (22:44 +0000)
mapsend.c

index 89629d1baaa5229e1d4eeb304bdd68f962ccd43f..b0133a7cc9aa0e7e2684ff6139e34e443ff171bd 100644 (file)
--- a/mapsend.c
+++ b/mapsend.c
@@ -25,8 +25,8 @@
 #include "mapsend.h"
 #include "magellan.h"
 
-static FILE *mapsend_file_in;
-static FILE *mapsend_file_out;
+static gbfile *mapsend_file_in;
+static gbfile *mapsend_file_out;
 static short_handle mkshort_handle;
 static short_handle wpt_handle;
 
@@ -71,62 +71,20 @@ static void
 mapsend_rd_init(const char *fname)
 {
        mapsend_init_opts(1);
-       mapsend_file_in = xfopen(fname, "rb", MYNAME);
+       mapsend_file_in = gbfopen_le(fname, "rb", MYNAME);
 }
 
 static void
 mapsend_rd_deinit(void)
 {
-       fclose(mapsend_file_in);
-}
-
-static 
-void 
-my_fread8(void *ptr, FILE *stream)
-{
-       unsigned char cbuf[8];
-       size_t rv;
-
-       rv = fread(cbuf, 8, 1, stream);
-       le_read64(ptr, cbuf);
-       
-}
-
-static 
-void
-my_fwrite8(void *ptr, FILE *stream)
-{
-       unsigned char cbuf[8];
-
-       le_read64(cbuf, ptr);
-       fwrite(cbuf, 8, 1, stream);
-}
-
-static 
-void 
-my_fread4(void *ptr, FILE *stream)
-{
-       unsigned int *iptr = ptr;
-       unsigned char cbuf[4];
-       size_t rv;
-
-       rv = fread(cbuf, 4, 1, stream);
-       *iptr = le_read32(cbuf);
-}
-
-static 
-size_t
-my_fwrite4(void *ptr, FILE *stream)
-{
-       int i = le_read32(ptr);
-       return fwrite(&i, 4, 1, stream);
+       gbfclose(mapsend_file_in);
 }
 
 static void
 mapsend_wr_init(const char *fname)
 {
        mapsend_init_opts(0);
-       mapsend_file_out = xfopen(fname, "wb", MYNAME);
+       mapsend_file_out = gbfopen(fname, "wb", MYNAME);
        mkshort_handle = mkshort_new_handle();
 
        wpt_handle = mkshort_new_handle();
@@ -139,7 +97,7 @@ mapsend_wr_init(const char *fname)
 static void
 mapsend_wr_deinit(void)
 {
-       fclose(mapsend_file_out);
+       gbfclose(mapsend_file_out);
        mkshort_del_handle(&mkshort_handle);
        mkshort_del_handle(&wpt_handle);
 }
@@ -148,50 +106,28 @@ static void
 mapsend_wpt_read(void)
 {
        char tbuf[256];
-       char name[257];
-       char comment[257];
-       char *p;
        int wpt_count, rte_count, rte_num;
-       unsigned char scount;
        int wpt_number;
        char wpt_icon;
        char wpt_status;
-       double wpt_alt;
-       double wpt_long;
-       double wpt_lat;
        waypoint *wpt_tmp;
        route_head *rte_head;
 
-       my_fread4(&wpt_count, mapsend_file_in);
+       wpt_count = gbfgetint32(mapsend_file_in);
        
        while (wpt_count--) {
                wpt_tmp = waypt_new();
 
-               if (fread(&scount, sizeof(scount), 1, mapsend_file_in) < 1) {
-                       fatal(MYNAME ": out of data reading %d waypoints\n",
-                                       wpt_count);
-               }
-               fread(tbuf, scount, 1, mapsend_file_in);
-               p = strncpy(name, tbuf, scount);
-               p[scount] = '\0';
-
-               fread(&scount, sizeof(scount), 1, mapsend_file_in);
-               fread(tbuf, scount, 1, mapsend_file_in);
-               p = strncpy(comment, tbuf, scount);
-               p[scount] = '\0';
-
-               my_fread4(&wpt_number, mapsend_file_in);
-               fread(&wpt_icon, sizeof(wpt_icon), 1, mapsend_file_in);
-               fread(&wpt_status, sizeof(wpt_status), 1, mapsend_file_in);
-               my_fread8(&wpt_alt, mapsend_file_in);
-               my_fread8(&wpt_long, mapsend_file_in);
-               my_fread8(&wpt_lat, mapsend_file_in);
-
-               wpt_tmp->shortname = xstrdup(name);
-               wpt_tmp->description = xstrdup(comment);
-               wpt_tmp->altitude = wpt_alt;
-               wpt_tmp->latitude = -wpt_lat;
-               wpt_tmp->longitude = wpt_long;
+               wpt_tmp->shortname = gbfgetpstr(mapsend_file_in);
+               wpt_tmp->description = gbfgetpstr(mapsend_file_in);
+
+               wpt_number = gbfgetint32(mapsend_file_in);
+               wpt_icon = gbfgetc(mapsend_file_in);
+               wpt_status = gbfgetc(mapsend_file_in);
+
+               wpt_tmp->altitude = gbfgetdbl(mapsend_file_in);
+               wpt_tmp->longitude = gbfgetdbl(mapsend_file_in);
+               wpt_tmp->latitude = -gbfgetdbl(mapsend_file_in);
 
                if (wpt_icon < 26)
                        sprintf(tbuf, "%c", wpt_icon + 'a');
@@ -203,43 +139,34 @@ mapsend_wpt_read(void)
        }
        
        /* now read the routes... */
-       my_fread4(&rte_count, mapsend_file_in);
+       rte_count = gbfgetint32(mapsend_file_in);
        
        while (rte_count--) {
                rte_head = route_head_alloc();
                route_add_head(rte_head);
                
                /* route name */
-               fread(&scount, sizeof(scount), 1, mapsend_file_in);
-               fread(tbuf, scount, 1, mapsend_file_in);
-               tbuf[scount] = '\0';
-               rte_head->rte_name = xstrdup(tbuf);
+               rte_head->rte_name = gbfgetpstr(mapsend_file_in);
                
                /* route # */
-               my_fread4(&rte_num, mapsend_file_in);
+               rte_num = gbfgetint32(mapsend_file_in);
                rte_head->rte_num = rte_num;
                
                /* points this route */
-               my_fread4(&wpt_count, mapsend_file_in);
+               wpt_count = gbfgetint32(mapsend_file_in);
                
                while (wpt_count--) {
                        wpt_tmp = waypt_new();
 
                        /* waypoint name */
-                       fread(&scount, sizeof(scount), 1, mapsend_file_in);
-                       fread(tbuf, scount, 1, mapsend_file_in);
-                       tbuf[scount] = '\0';
-
-                       wpt_tmp->shortname = xstrdup(tbuf);
+                       wpt_tmp->shortname = gbfgetpstr(mapsend_file_in);
                        
                        /* waypoint # */
-                       my_fread4(&wpt_number, mapsend_file_in);
-                       my_fread8(&wpt_long, mapsend_file_in);
-                       my_fread8(&wpt_lat, mapsend_file_in);
-                       fread(&wpt_icon, sizeof(wpt_icon), 1, mapsend_file_in);
+                       wpt_number = gbfgetint32(mapsend_file_in);
+                       wpt_tmp->longitude = gbfgetdbl(mapsend_file_in);
+                       wpt_tmp->latitude = -gbfgetdbl(mapsend_file_in);
 
-                       wpt_tmp->longitude = wpt_long;
-                       wpt_tmp->latitude = -wpt_lat;
+                       gbfread(&wpt_icon, 1, sizeof(wpt_icon), mapsend_file_in);
 
                        if (wpt_icon < 26)
                                sprintf(tbuf, "%c", wpt_icon + 'a');
@@ -255,56 +182,40 @@ mapsend_wpt_read(void)
 static void
 mapsend_track_read(void)
 {
-       char *trk_name;
-       unsigned char scount;
        unsigned int trk_count;
-       double wpt_long;
-       double wpt_lat;
-       double wpt_alt;
-       int i = 0;
-       float f = 0;
-       int time;
        int valid;
        unsigned char centisecs;
        route_head *track_head;
        waypoint *wpt_tmp;
 
-       fread(&scount, sizeof(scount), 1, mapsend_file_in);
-       trk_name = xmalloc(scount + 1);
-       fread(trk_name, scount, 1, mapsend_file_in);
-       trk_name[scount] = '\0';
-       my_fread4(&trk_count, mapsend_file_in);
-
        track_head = route_head_alloc();
-       track_head->rte_name = trk_name;
+       track_head->rte_name = gbfgetpstr(mapsend_file_in);
        track_add_head(track_head);
 
+       trk_count = gbfgetuint32(mapsend_file_in);
+
        while (trk_count--) {
-               my_fread8(&wpt_long, mapsend_file_in);
-               my_fread8(&wpt_lat, mapsend_file_in);
+               wpt_tmp = waypt_new();
+               
+               wpt_tmp->longitude = gbfgetdbl(mapsend_file_in);
+               wpt_tmp->latitude = -gbfgetdbl(mapsend_file_in);
+               
                if (mapsend_infile_version < 36) { /* < version 4.0 */
-                       my_fread4(&i, mapsend_file_in);
-                       wpt_alt = i;
+                       wpt_tmp->altitude = gbfgetint32(mapsend_file_in);
                } else {
-                       my_fread4(&f, mapsend_file_in);
-                       wpt_alt = f;
+                       wpt_tmp->altitude = gbfgetflt(mapsend_file_in);
                }
-               my_fread4(&time, mapsend_file_in);
-               my_fread4(&valid, mapsend_file_in);
+               wpt_tmp->creation_time = gbfgetint32(mapsend_file_in);
+               valid = gbfgetint32(mapsend_file_in);
 
                /* centiseconds only in >= version 3.0 */
                if (mapsend_infile_version >= 34) {
-                       fread(&centisecs, 1, 1, mapsend_file_in);
+                       gbfread(&centisecs, 1, 1, mapsend_file_in);
                } else {
                        centisecs = 0;
                }
-
-               wpt_tmp = waypt_new();
-               wpt_tmp->latitude = -wpt_lat;
-               wpt_tmp->longitude = wpt_long;
-               wpt_tmp->creation_time = time;
                wpt_tmp->centiseconds = centisecs;
-               wpt_tmp->altitude = wpt_alt;
+               
                track_add_wpt(track_head, wpt_tmp);
        }
 }
@@ -321,7 +232,7 @@ mapsend_read(void)
         * strings, each member has to be read in one at a time.  Grrr.
         */
 
-       len = fread(&hdr, 1, sizeof(hdr), mapsend_file_in);
+       len = gbfread(&hdr, 1, sizeof(hdr), mapsend_file_in);
        is_fatal(len < sizeof(hdr), MYNAME ": No mapsend or empty file!");
        
        type = le_read16(&hdr.ms_type);
@@ -350,11 +261,8 @@ mapsend_read(void)
 static void
 mapsend_waypt_pr(const waypoint *waypointp)
 {
-       int n;
        unsigned char c;
        double falt;
-       double flong;
-       double flat;
        static int cnt = 0;
        const char *iconp = NULL;
        const char *sn = global_opts.synthesize_shortnames ? 
@@ -379,11 +287,8 @@ mapsend_waypt_pr(const waypoint *waypointp)
 
        
        tmp = mkshort(wpt_handle, sn);
-       c = tmp ? strlen(tmp) : 0;
-       fwrite(&c, 1, 1, mapsend_file_out);
-       fwrite(tmp, c, 1, mapsend_file_out);
-       if (tmp)
-               xfree(tmp);
+       gbfputpstr(tmp, mapsend_file_out);
+       if (tmp) xfree(tmp);
 
        tmp = waypointp->description;
        if (tmp)
@@ -392,12 +297,11 @@ mapsend_waypt_pr(const waypoint *waypointp)
                c = 0;
 
        if (c > 30) c = 30;
-       fwrite(&c, 1, 1, mapsend_file_out);
-       fwrite(tmp, c, 1, mapsend_file_out);
+       gbfputc(c, mapsend_file_out);
+       gbfwrite(tmp, c, 1, mapsend_file_out);
 
        /* #, icon, status */
-       n = ++cnt;
-       my_fwrite4(&n, mapsend_file_out);
+       gbfputint32(++cnt, mapsend_file_out);
 
        if (waypointp->icon_descr) {
                iconp = mag_find_token_from_descr(waypointp->icon_descr);
@@ -418,26 +322,21 @@ mapsend_waypt_pr(const waypoint *waypointp)
                }
        }
 
-       fwrite(&c, 1, 1, mapsend_file_out);
-       c = 1;
-       fwrite(&c, 1, 1, mapsend_file_out);
+       gbfwrite(&c, 1, 1, mapsend_file_out);
+       gbfputc(1, mapsend_file_out);
 
        falt = waypointp->altitude;
        if (falt == unknown_alt)
                falt = 0;
-       my_fwrite8(&falt, mapsend_file_out);
+       gbfputdbl(falt, mapsend_file_out);
 
-       flong = waypointp->longitude;
-       my_fwrite8(&flong, mapsend_file_out);
-       flat = -waypointp->latitude;
-       my_fwrite8(&flat, mapsend_file_out);
+       gbfputdbl(waypointp->longitude, mapsend_file_out);
+       gbfputdbl(-waypointp->latitude, mapsend_file_out);
 }
 
 static void 
 mapsend_route_hdr(const route_head *rte)
 {
-       int i;
-       unsigned char c;
        char * rname;
        
        /* route name -- mapsend really seems to want something here.. */
@@ -445,22 +344,15 @@ mapsend_route_hdr(const route_head *rte)
                rname = xstrdup("Route");
        else
                rname = xstrdup(rte->rte_name);
-
-       c = strlen(rname);
-       
-       fwrite(&c, 1, 1, mapsend_file_out);
-       fwrite(rname, c, 1, mapsend_file_out);
+       gbfputpstr(rname, mapsend_file_out);
 
        xfree(rname);
        
        /* route # */
-       i = rte->rte_num;
-       my_fwrite4(&i, mapsend_file_out);
+       gbfputint32(rte->rte_num, mapsend_file_out);
        
-       i = rte->rte_waypt_ct;
-
        /* # of waypoints to follow... */
-       my_fwrite4(&i, mapsend_file_out);
+       gbfputint32(rte->rte_waypt_ct, mapsend_file_out);
 }
 
 static void 
@@ -474,23 +366,19 @@ mapsend_route_disp(const waypoint *waypointp)
 {
        unsigned char c;
        const char *iconp;
-       double dbl;
 
        route_wp_count++;
        
        /* waypoint name */
        c = waypointp->shortname ? strlen(waypointp->shortname) : 0;
-       fwrite(&c, 1, 1, mapsend_file_out);
-       fwrite(waypointp->shortname, c, 1, mapsend_file_out);
+       gbfwrite(&c, 1, 1, mapsend_file_out);
+       gbfwrite(waypointp->shortname, c, 1, mapsend_file_out);
        
        /* waypoint number */
-       my_fwrite4(&route_wp_count, mapsend_file_out);
-
-       dbl = waypointp->longitude;
-       my_fwrite8(&dbl, mapsend_file_out);
+       gbfputint32(route_wp_count, mapsend_file_out);
 
-       dbl = -waypointp->latitude;
-       my_fwrite8(&dbl, mapsend_file_out);
+       gbfputdbl(waypointp->longitude, mapsend_file_out);
+       gbfputdbl(-waypointp->latitude, mapsend_file_out);
 
        if (waypointp->icon_descr) {
                iconp = mag_find_token_from_descr(waypointp->icon_descr);
@@ -502,7 +390,7 @@ mapsend_route_disp(const waypoint *waypointp)
        } else  {
                c = 0;
        }
-       fwrite(&c, 1, 1, mapsend_file_out);
+       gbfwrite(&c, 1, 1, mapsend_file_out);
 }
 
 void mapsend_track_hdr(const route_head * trk)
@@ -514,7 +402,6 @@ void mapsend_track_hdr(const route_head * trk)
        char *verstring = "30";
        queue *elem, *tmp;
        char *tname;
-       unsigned char c;
        int i;
        mapsend_hdr hdr = {13, "4D533334 MS", "30", ms_type_track, {0, 0, 0}};
        
@@ -535,18 +422,15 @@ void mapsend_track_hdr(const route_head * trk)
        hdr.ms_version[0] = verstring[0];
        hdr.ms_version[1] = verstring[1];
 
-       fwrite(&hdr, sizeof(hdr), 1, mapsend_file_out);
+       gbfwrite(&hdr, sizeof(hdr), 1, mapsend_file_out);
 
        /* track name */
        if (!trk->rte_name)
                tname = xstrdup("Track");
        else
                tname = xstrdup(trk->rte_name);
+       gbfputpstr(tname, mapsend_file_out);
 
-       c = strlen(tname);
-       
-       fwrite(&c, 1, 1, mapsend_file_out);
-       fwrite(tname, c, 1, mapsend_file_out);
        xfree(tname);
        
        /* total nodes (waypoints) this track */
@@ -555,18 +439,14 @@ void mapsend_track_hdr(const route_head * trk)
                i++;
        }               
 
-       my_fwrite4(&i, mapsend_file_out);
+       gbfputint32(i, mapsend_file_out);
        
 }
 
 void mapsend_track_disp(const waypoint * wpt)
 {
        unsigned char c;
-       double dbl;
-       int i;
        int t;
-       int f;
-       int valid = 1;
        static int last_time;
 
        /*
@@ -585,35 +465,31 @@ void mapsend_track_disp(const waypoint * wpt)
        }
        
        /* x = longitude */
-       dbl = wpt->longitude;
-       my_fwrite8(&dbl, mapsend_file_out);
+       gbfputdbl(wpt->longitude, mapsend_file_out);
 
        /* x = latitude */
-       dbl = -wpt->latitude;
-       my_fwrite8(&dbl, mapsend_file_out);
+       gbfputdbl(-wpt->latitude, mapsend_file_out);
 
        /* altitude 
         * in V4.0+ this field is a float, it was previously an int
         */
        if (trk_version < 40) {
-               i = (int) wpt->altitude;
-               my_fwrite4(&i, mapsend_file_out);
+               gbfputint32((int) wpt->altitude, mapsend_file_out);
        } else {
-               f = (float) wpt->altitude;
-               my_fwrite4(&f, mapsend_file_out);
+               gbfputflt((float) wpt->altitude, mapsend_file_out);
        }
        
        /* time */
-       my_fwrite4(&t, mapsend_file_out);
+       gbfputint32(t, mapsend_file_out);
        last_time = t;
 
        /* validity */
-       my_fwrite4(&valid, mapsend_file_out);
+       gbfputint32(1, mapsend_file_out);
 
        /* 0 centiseconds */
        if (trk_version >= 30) {
                c = wpt->centiseconds;
-               fwrite(&c, 1, 1, mapsend_file_out);
+               gbfwrite(&c, 1, 1, mapsend_file_out);
        }
 }
 
@@ -633,17 +509,16 @@ mapsend_wpt_write(void)
        if (global_opts.objective == trkdata) {
                mapsend_track_write();
        } else {
-               fwrite(&hdr, sizeof(hdr), 1, mapsend_file_out);
+               gbfwrite(&hdr, sizeof(hdr), 1, mapsend_file_out);
 
                if (global_opts.objective == wptdata) {
-                       my_fwrite4(&wpt_count, mapsend_file_out);
+                       gbfputint32(wpt_count, mapsend_file_out);
                        waypt_disp_all(mapsend_waypt_pr);
                } else 
                if (global_opts.objective == rtedata) {
 
                        /* # of points - all routes */
-                       n = route_waypt_count();
-                       my_fwrite4(&n, mapsend_file_out);
+                       gbfputint32(route_waypt_count(), mapsend_file_out);
 
                        /* write points - all routes */
                        route_disp_all(mapsend_noop, mapsend_noop, mapsend_waypt_pr);
@@ -651,7 +526,7 @@ mapsend_wpt_write(void)
                
                n = route_count();
 
-               my_fwrite4(&n, mapsend_file_out);
+               gbfputint32(n, mapsend_file_out);
        
                if (n)
                        route_disp_all(mapsend_route_hdr, mapsend_noop, mapsend_route_disp);